package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    //注入shoppingCartMapper
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    //注入dishMapper
    @Autowired
    private DishMapper dishMapper;

    //注入setmealMapper
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 天机哎购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //数据库是否已存在数据库
        //DTO转实体
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置userId
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //查询shopping_cart表（动态查询）
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //判断是否存在
        if(list !=null && list.size() > 0){
            //存在，则增加数量
            ShoppingCart cart = list.get(0);    //因为使用userId 和 setmealId,dishFlavor来查询，所以只会查出一条数据
            //设置数量
            cart.setNumber(cart.getNumber()+1);
            //更新数据库
            shoppingCartMapper.updateNumberById(cart);
        }else{
            //不存在，则插入一条数据
            //判断插入菜品还是套餐
            if(shoppingCartDTO.getDishId() != null) {
                //菜品
                //查询dish表，获得name,image,price
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
            }else{
                //套餐
                //查询setmeal表，获得name,image,price
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
            }
            //设置数量
            shoppingCart.setNumber(1);
            //插入数据库
            shoppingCartMapper.insert(shoppingCart);

        }
    }

    /**
     * 根据用户id查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //根据用户id查询购物车
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //返回结果
        return list;
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //查询当前商品数量
        //DTO转实体
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if(list != null && list.size() > 0){
            shoppingCart = list.get(0);
            Integer number = shoppingCart.getNumber();

            //若大于1，则更新数量 -1
            if(number > 1){
                shoppingCart.setNumber(number-1);
                shoppingCartMapper.updateNumberById(shoppingCart);
                return;
            }

            //若等于1，则直接删除购物车中的商品
            if(number == 1){
                shoppingCartMapper.delete(shoppingCart);
            }
        }

    }

    /**
     * 清空用户购物车
     */
    @Override
    public void clean() {
        //获取当前登录用户id
        Long userId = BaseContext.getCurrentId();

        //删除该用户名下购物车所有商品
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        shoppingCartMapper.delete(shoppingCart);
    }

}
